package com.kedll.library.location;

import android.app.Application;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

import com.kedll.library.R;
import com.kedll.library.dialog.ECAlertDialog;
import com.kedll.library.location.entity.LocationEntity;
import com.kedll.library.location.listener.ILocationEntity;
import com.kedll.library.location.listener.OnLocationChangeListener;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.SharePreUtils;
import com.kedll.library.util.StringUtil;

/**
 * 定位请求工具类
 *
 * @author QiQi
 * @date 2016/10/26
 */

public class RequestLocationHelper implements OnLocationChangeListener, LocationInterface {

    /**
     * 上下文
     */
    private FragmentActivity mActivity;
    private LocationInterface locationInterface;
    /**
     * 判断是否存在历史数据
     */
    private boolean isFirst = false;
    private ILocationEntity app;
    private LocationEntity curEntity;
    private OnLocationCompletedListener locationCompletedListener;
    private TextView tvCity;

    public RequestLocationHelper(FragmentActivity activity, OnLocationCompletedListener locationCompletedListener) {
        this.mActivity = activity;
        this.locationCompletedListener = locationCompletedListener;
        Application application = activity.getApplication();
        if (application instanceof ILocationEntity) {
            app = (ILocationEntity) application;
        } else {
            throw new IllegalStateException("必须使用自定义Application，并且实现ILocationEntity");
        }
    }

    public void setLocationInterface(LocationInterface locationInterface) {
        this.locationInterface = locationInterface;
    }

    public void setCityTextView(TextView cityTxt) {
        this.tvCity = cityTxt;
    }

    /**
     * 定位回调
     *
     * @param entity 位置信息
     */
    @Override
    public void onLocationChangeListener(final LocationEntity entity) {
        MyLog.getInstance().dismissDialog();
        if (isFirst) {
            saveLocation(entity);
        } else {
            //当前定位信息和历史的定位信息不相同， 询问用户是否切换城市
            if (StringUtil.isNotEqual(curEntity.getCity(), entity.getCity())) {
                ECAlertDialog dialog = ECAlertDialog.buildAlert(mActivity,
                        String.format("当前定位的城市是%s, 是否切换？",
                                entity.getCity()),
                        "否", "是",
                        (dialog1, which) -> dialog1.dismiss(),
                        (dialog12, which) -> {
                            dialog12.dismiss();
                            //同意切换城市，并重新请求数据
                            RequestLocationHelper.this.saveLocation(entity);
                        });
                dialog.show();
            } else {
                saveLocation(entity);
            }
        }
    }

    /**
     * 保存新定位信息，并请求数据
     *
     * @param entity
     */
    private void saveLocation(LocationEntity entity) {
        curEntity = entity;
        app.setLocation(entity);
        locationCompletedListener.onCompleted(entity);
        SharePreUtils.saveLocation(mActivity, entity);
        if (tvCity != null) {
            tvCity.setText(entity.getCity());
        }
    }

    /**
     * 开启定位
     */
    @Override
    public void startLocation(long interval) {
        //提取保存的定位数据
        curEntity = SharePreUtils.getLocation(mActivity);
        //如果为空，则表示第一次定位，显示正在定位提示框
        if (curEntity == null) {
            isFirst = true;
            MyLog.getInstance().showProgressDialog(mActivity, R.string.string_locationing);
            if (tvCity != null) {
                tvCity.setText(curEntity == null ? "定位中..." : curEntity.getDistrict());
            }
            locationInterface.startLocation(interval);
        } else {
            //有历史数据，设置全局
            app.setLocation(curEntity);
            //刷新数据
            locationCompletedListener.setHistoryLocation(curEntity);
        }

    }

    /**
     * 停止定位 并销毁
     */
    @Override
    public void stopLocation() {
        mActivity = null;
        locationInterface.stopLocation();
        locationCompletedListener = null;
        locationInterface = null;
    }

    public interface OnLocationCompletedListener {
        /**
         * 定位成功
         *
         * @param entity
         */
        void onCompleted(LocationEntity entity);


        /**
         * 显示历史定位数据
         *
         * @param entity
         */
        void setHistoryLocation(LocationEntity entity);
    }
}
